package com.kksoft.android.xiaoliaobaike;

import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.kksoft.android.xiaoliaobaike.fetcher.SeriesFetcher;
import com.kksoft.android.xiaoliaobaike.model.Series;

public class MainActivity extends FinalActivity {

	public static final String TAG = "XiaoLiaoBeiKe";

	// @ViewInject(id = R.id.seriesLv)
	// ListView seriesLv;
	@ViewInject(id = R.id.but)
	Button but;
	@ViewInject(id = R.id.scroll)
	ScrollView scroll;
	@ViewInject(id = R.id.txt)
	TextView txt;

	ProgressDialog progressDialog;
	SeriesFetcher seriesFetcher;
	Handler seriesHandler;

	int nextPage = 1;

	boolean running = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		seriesHandler = new SeriesHandler();
		seriesFetcher = new SeriesFetcher(seriesHandler);

		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("正在解析数据...");
		progressDialog.setCancelable(false);

		but.setText("解析第" + nextPage + "页");

		but.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (running) {
					Toast.makeText(MainActivity.this, "正在处理请求，请稍后...",
							Toast.LENGTH_LONG).show();
					return;
				}
				seriesFetcher.fetch(nextPage++);
				but.setText("解析第" + nextPage + "页");
			}
		});
	}

	private class SeriesHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			MainActivity.this.progressDialog.setCancelable(false);
			switch (msg.what) {
			case Const.MSG_URL_NOT_FOUND:
				running = false;

				// Toast.makeText(MainActivity.this,
				// "已经是最后一页。", Toast.LENGTH_LONG)
				// .show();
				but.setText("已经是最后一页");
				but.setEnabled(false);
				MainActivity.this.progressDialog.hide();
				break;
			case Const.MSG_SERIES_FETCH_START:
				running = true;
				MainActivity.this.progressDialog.setMessage("正在获取数据...");
				MainActivity.this.progressDialog.show();
				break;
			case Const.MSG_SERIES_FETCH_ERROR:
				running = false;
				MainActivity.this.progressDialog.setCancelable(true);
				MainActivity.this.progressDialog
						.setMessage("获取数据出错：" + msg.obj);
				MainActivity.this.progressDialog.show();
				break;
			case Const.MSG_SERIES_FETCH_FINISH:
				running = false;
				MainActivity.this.progressDialog.setMessage("获取数据完成。");
				MainActivity.this.progressDialog.hide();
				break;
			case Const.MSG_SERIES_ANALYSIS_START:
				MainActivity.this.progressDialog.setMessage("正在解析数据...");
				MainActivity.this.progressDialog.show();
				break;
			case Const.MSG_SERIES_ANALYSIS_ERROR:
				running = false;
				MainActivity.this.progressDialog.setCancelable(true);
				MainActivity.this.progressDialog
						.setMessage("解析数据出错：" + msg.obj);
				MainActivity.this.progressDialog.show();
				break;
			case Const.MSG_SERIES_ANALYSIS_FINISH:
				MainActivity.this.progressDialog.setMessage("解析数据完成。");
				MainActivity.this.progressDialog.hide();
				break;
			case Const.MSG_SERIES_LIST:
				@SuppressWarnings("unchecked")
				List<Series> seriesList = (List<Series>) msg.obj;
				Toast.makeText(MainActivity.this,
						"新增数据：" + seriesList.size() + "条。", Toast.LENGTH_LONG)
						.show();
				if (seriesList.size() > 0) {
					// txt.setText("");
					StringBuffer sb = new StringBuffer();
					sb.append("第" + (nextPage - 1) + "页\r\n");
					sb.append("===========\r\n");
					for (Series s : seriesList) {
						sb.append("title:" + s.getTitle() + "\r\n");
						sb.append("seriesUrl:" + s.getSeriesUrl() + "\r\n");
						sb.append("coverUrl:" + s.getCoverUrl() + "\r\n");
						sb.append("describe:" + s.getDescribe() + "\r\n");
						sb.append("total:" + s.getTotal() + "\r\n");
						sb.append("firstUrl:" + s.getFirstUrl() + "\r\n");
						sb.append("----------\r\n");
					}
					txt.setText(txt.getText() + sb.toString());
					// TODO: 滚动到最下面
					// scroll.scrollBy(0, scroll.getHeight());
				}
				break;
			}
		}
	}

	@Override
	protected void onDestroy() {
		progressDialog.dismiss();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.menu_settings:
			Toast.makeText(this, "亲，这个功能还木有实现哦。。。", Toast.LENGTH_LONG).show();
			break;
		case R.id.menu_exit:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}